package com.lzj.demo.design.memo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 管理员类: 用于记录备忘信息,比如一系列的顺序执行了什么操作或者某个版本下的内容
 * */
public class Admin {
    private int currentIndex;
    private List<ConfigMemo> configMemoList = new ArrayList<>();
    private Map<String,ConfigMemo> configMemoMap = new ConcurrentHashMap<>();

    /**
     * 追加
     * */
    public void append(ConfigMemo configMemo){
        configMemoList.add(configMemo);
        configMemoMap.put(configMemo.getConfigFile().getVersionNo(),configMemo);
        currentIndex++;
    }
    /**
     * 回滚
     * */
    public ConfigMemo undo(){
        if(--currentIndex <= 0 ){
              return configMemoList.get(0);
        }
        return configMemoList.get(currentIndex);
    }
    /**
     * 返回
     * */
    public ConfigMemo redo(){
        if(++currentIndex > configMemoList.size()){
            return configMemoList.get(configMemoList.size()-1);
        }
        return configMemoList.get(currentIndex);
    }
    /**
     * 定向获取
     * */
    public ConfigMemo get(String versionNo){
        return configMemoMap.get(versionNo);
    }
}
